#Basic threading example pulled from python.org
from multiprocessing import Process, Pipe

def calc(array, connection):
	for x in array:
		array[x] = array[x]*5
	connection.send(array)
	return 0

def buildarray(val):
	return range(val)

if __name__== '__main__':

	array = buildarray(5)
	print array
	connections = Pipe()
	a = connections[0]
	b = connections[1]
	p = Process(target=calc, args=(array,b,))
	p.start()
	val = a.recv()
	p.terminate()
	print val[3]

from multiprocessing import Process
from multiprocessing.queues import SimpleQueue

#My attempt to implement threading for the model.
#Useful to help understand how model2D.py does multithreading,
#it's essentially the same implementation.
def calc2(array, queue, position):
	for x in range(len(array)):
		array[x] = array[x]*5
	queue.put([array, position])
	return 0

if __name__ == '__main__':
	extra = 1
	while extra != 0:
		size = int(raw_input('Size?: '))
		threads = int(raw_input('Threads?: '))
		extra = size % threads

	sizeperthread = size / threads
	array = buildarray(size)
	threadarray = []
	q = SimpleQueue()
	increment = 0
	for x in range(size / sizeperthread):
		threadarray.append(Process(target=calc2, args=(array[increment:(increment + sizeperthread)],q,x,)))
		threadarray[x].start()
		increment += sizeperthread

	resultarray = []
	returnedthreads = 0
	while returnedthreads != threads:
		if not q.empty():
			val = q.get()
			resultarray.append(val)
			returnedthreads += 1
		
	final = []
	increment = -1	
	val = 0
	while increment != threads:
		for x in resultarray:
			if x[1] == increment + 1:
				final.extend(x[0])
				val = x[1]
		increment += 1
	
		
	

	
